package cn.edu.ujn.lishlab3.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.lishlab3.dao.Product;
import cn.edu.ujn.lishlab3.service.IProductService;
import cn.edu.ujn.util.Page;


@Controller
public class ProductController {
	@Autowired
	private IProductService productService;
	
	@RequestMapping(value = "/product/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,Integer id,
			String name,Double price, Model model) {
		// 查询客户
		Page<Product> product = productService.findProductList(page, rows, id, name, price);
		model.addAttribute("page", product);

		model.addAttribute("id", id);
		model.addAttribute("name", name);
		model.addAttribute("price", price);
		return "product";
	}
	
	@RequestMapping(value = "/product/create.action")
	@ResponseBody
	public String productCreate(Product product,Integer id,String name,Double price) {
		// 将当前用户id存储在客户对象中
        product.setId(id);
        product.setName(name);
        product.setPrice(price);
		// 执行Service层中的创建方法，返回的是受影响的行数
		int rows = this.productService.insertSelective(product);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}
	
	@RequestMapping("/product/getProductById.action")
	@ResponseBody
	public Product getProductById(Integer id) {
	    Product product = this.productService.selectByPrimaryKey(id);
	    return product;
	}
	
	/**
	 * 更新产品
	 */
	@RequestMapping("/product/update.action")
	@ResponseBody
	public String productUpdate(Product product,Integer id,String name,Double price) {
		product.setId(id);
        product.setName(name);
        product.setPrice(price);
	    int rows = this.productService.updateByPrimaryKeySelective(product);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	

	//删除用户
		@RequestMapping("/product/delete.action")
		@ResponseBody
		public String customerDelete(Integer id) {
		    int rows = productService.deleteProduct(id);
		    if(rows > 0){			
		        return "OK";
		    }else{
		        return "FAIL";			
		    }
		}
}


